<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 达达 <ccbox_net@163.com>
// +----------------------------------------------------------------------
namespace app\goods\service;

use app\goods\lib\service\BaseService;
use app\goods\model\BrandModel;

/**
 * 品牌服务
 * 
 */
class BrandService extends BaseService
{
    /**
     * 设置模型
     * @return string
     */
    protected function setModel(): string
    {
        return BrandModel::class;
    }

    /**
     * 处理搜索参数
     * @param array $filter
     * @param array|string[] $with
     * 
     * @return array
     * 
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function handleFilter(array $param)
    {
        $filter = [];

        // status
        $status = empty($param['status']) ? '' : $param['status'];
        if (!empty($status) && in_array($status, ['yes', 'no'])) {
            $status = $status == 'yes' ? 1 : 0;
            $filter[] = ['status','=', $status];
        }

        // keyword
        $keyword = empty($param['keyword']) ? '' : $param['keyword'];
        if (!empty($keyword)) {
            $filter[] = ['title', 'like', "%" . trim($keyword) . "%"];
        }

        // 排序
        $sort_start = empty($param['id_start']) ? '' : $param['id_start'];
        if (!empty($sort_start)) {
            $filter[] = ['id', '>=', $sort_start];
        }
        $sort_end = empty($param['id_end']) ? '' : $param['id_end'];
        if (!empty($sort_end)) {
            $filter[] = ['id', '<=', $sort_end];
        }

        return parent::handleFilter($filter);
    }

    /**
     * 搜索列表
     * @param array $filter
     * @param array|string[] $field
     * @param array|string[] $sort
     * @param int $limit
     * @param array|string[] $with
     * 
     * @return \think\Paginator
     * 
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSearchList(array $filter, array $field = ['*'], $sort = [], int $limit = 0, array $with = [])
    {
        $sort = $this->handleSortField($sort);

        return $this->getListPage($filter, $field, $sort, $limit, $with);
    }

}